from sqlalchemy.orm import Session
from schema import AI_Consultation_schema
from model import AI_Consultation_model



def get_AI_Consultation(db: Session, ID: int):
    return db.query(AI_Consultation_model.Ai_consultation).filter(AI_Consultation_model.Ai_consultation.ID == ID).first()


def get_all_AI_Consultation(db: Session):
    return db.query(AI_Consultation_model.Ai_consultation).all()


def delete_AI_Consultation(db: Session, ID):
    to_delete = db.query(AI_Consultation_model.Ai_consultation).filter(AI_Consultation_model.Ai_consultation.ID == ID)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_AI_Consultation(db: Session, AI_Consultation: AI_Consultation_schema.Ai_consultation):
    db_AI_Consultation = AI_Consultation_model.Ai_consultation(
        AdminID=AI_Consultation.AdminID,
        LesseeID=AI_Consultation.LesseeID,
        QueryContent=AI_Consultation.QueryContent,
        ReplyContent=AI_Consultation.ReplyContent,
        CreationTime=AI_Consultation.CreationTime,
    )
    db.add(db_AI_Consultation)
    db.commit()
    db.refresh(db_AI_Consultation)
    return db_AI_Consultation


def update_AI_Consultation(db: Session, ID, field_name, new_value):
    db_AI_Consultation = db.query(AI_Consultation_model.Ai_consultation).filter(AI_Consultation_model.Ai_consultation.ID == ID).first()
    setattr(db_AI_Consultation, field_name, new_value)
    db.commit()
    db.refresh(db_AI_Consultation)
    return db_AI_Consultation
